package com.ruoyi.common.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

public class GoogleTest {

    /*
     * gmail邮箱SSL方式
     */
    private static void gmailssl(Properties props) {
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        props.put("mail.debug", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
    }


    //gmail邮箱的TLS方式
    private static void gmailtls(Properties props) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
    }

    /**
     * 创建邮件内容 需科学上网
     *
     * @param sentToEmail 接收人邮箱
     * @return
     * @throws Exception
     */
    public static boolean getMimeMessage(String sentToEmail,String code) throws Exception {
        //1.创建一封邮件的实例对象
        Properties props = new Properties();
        //选择TLS方式
//        gmailtls(props);
        //选择ssl方式
        gmailssl(props);

        final String username = "eastcard.office@gmail.com";// gmail 邮箱
        final String password = "mztz yukh iewi cdrb";// Google应用专用密码
        // 当做多商户的时候需要使用getInstance, 如果只是一个邮箱发送的话就用getDefaultInstance
        // Session.getDefaultInstance 会将username,password保存在session会话中
        // Session.getInstance 不进行保存
        Session session = Session.getInstance(props,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

        MimeMessage msg = new MimeMessage(session);
        //2.设置发件人地址
        msg.setFrom(new InternetAddress(username));
//        msg.setFrom(new InternetAddress(sentToEmail));
        /**
         * 3.设置收件人地址（可以增加多个收件人、抄送、密送），即下面这一行代码书写多行
         * MimeMessage.RecipientType.TO:发送
         * MimeMessage.RecipientType.CC：抄送
         * MimeMessage.RecipientType.BCC：密送
         */
        msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(sentToEmail));
        //4.设置邮件主题
        msg.setSubject("Your Ubcard Verification Code", "UTF-8");
        // 6. 创建文本"节点"
        MimeBodyPart text = new MimeBodyPart();
        // 这里添加图片的方式是将整个图片包含到邮件内容中, 实际上也可以以 http 链接的形式添加网络图片
        text.setContent(
            "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <style type=\"text/css\">\n" +
                "        @media screen and (max-width: 600px) {\n" +
                "            .container { width: 100%!important; }\n" +
                "            .content { padding: 15px!important; }\n" +
                "        }\n" +
                "        .hidden-content { display: none; }\n" +
                "        .toggle-checkbox { display: none; }\n" +
                "        .toggle-checkbox:checked + .hidden-content { display: block; }\n" +
                "        .toggle-checkbox:checked ~ .content { display: none; }\n" +
                "        .toggle-button {\n" +
                "            color: blue;\n" +
                "            text-decoration: underline;\n" +
                "            text-align: center;\n" +
                "            display: inline-block;\n" +
                "            cursor: pointer;\n" +
                "        }\n" +
                "        .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .full-content .hidden-content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .full-content .hidden-content {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        .toggle-checkbox:checked ~ .content {\n" +
                "            display: none;\n" +
                "        }\n" +
                "        .content {\n" +
                "            height: 400px;\n" +
                "            .position {\n" +
                "                padding: 200px 0;\n" +
                "            }\n" +
                "        }\n" +
                "        .container {\n" +
                "            display: flex;\n" +
                "            flex-direction: column;\n" +
                "            justify-content: center;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <input type=\"checkbox\" id=\"toggle-1\" class=\"toggle-checkbox\">\n" +
                "        <div class=\"content\">\n" +
                "            <div class=\"position\">\n" +
                "                <p>...</p>\n" +
                "                <span>[邮件部分隐藏]</span>\n" +
                "                <!-- 展开按钮 -->\n" +
                "                <label for=\"toggle-1\" class=\"toggle-button\">点击展开内容</label>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "        <div class=\"full-content\">\n" +
                "            <!-- 隐藏内容 -->\n" +
                "            <div class=\"hidden-content\">\n" +
                "\t\t\t\t<img src='cid:' width='200' alt='logo' />\n" +
                "                <p><strong style=\"font-size: 18px;\">Dear Valued Customer,</strong></p>\n" +
                "                <p>Your verification code is: <strong style=\"font-size: 22px;\">123456</strong></p>\n" +
                "                <p><br></p>\n" +
                "                <p>This code will expire within 5 minutes. Never provide this code to anyone.\n" +
                "\t\t\t\tIf you have any question or need support, please contact our customer service team immediately.</p>\n" +
                "                <p><br></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">Best regards,</strong></p>\n" +
                "                <p><strong style=\"font-size: 18px;\">The UBPay Team</strong></p>\n" +
                "\t\t\t\t<p style=\"font-size: 16px;font-style: italic;\">his is an automated message, please do not reply.</p>\n" +
                "                <p style=\"font-size: 14px;\">© 2025 UBPay, All Rights Reserved.</p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>\n",
            "text/html;charset=UTF-8"
        );

//        text.setContent(
//            "<!DOCTYPE html>\n" +
//                "<html>\n" +
//                "<head>\n" +
//                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
//                "<style type=\"text/css\">\n" +
//                "  @media screen and (max-width: 600px) {\n" +
//                "    .container { width: 100%!important; }\n" +
//                "    .content { padding: 15px!important; }\n" +
//                "  }\n" +
//                "  .hidden-content { display: none; }\n" +
//                "  .toggle-checkbox { display: none; }\n" +
//                "  .toggle-checkbox:checked + .hidden-content { display: block; }\n" +
//                "  .toggle-checkbox:checked ~ .content { display: none; }\n" +
//                "  .toggle-button {\n" +
//                "        color: blue;\n" +
//                "        text-decoration: underline;\n" +
//                "        text-align: center;\n" +
//                "        display: inline-block;\n" +
//                "        cursor: pointer;\n" +
//                "  }\n" +
//                "  .full-content {\n" +
//                "    display: block;\n" +
//                "  }\n" +
//                "  .full-content .hidden-content {\n" +
//                "    display: none;\n" +
//                "  }\n" +
//                "  .toggle-checkbox:checked ~ .full-content {\n" +
//                "    display: block;\n" +
//                "  }\n" +
//                "  .toggle-checkbox:checked ~ .full-content .hidden-content {\n" +
//                "    display: block;\n" +
//                "  }\n" +
//                "  .toggle-checkbox:checked ~ .content {\n" +
//                "    display: none;\n" +
//                "  }\n" +
//                "</style>\n" +
//                "</head>\n" +
//                "<body>\n" +
//                "  <div class=\"container\">\n" +
//                "    <input type=\"checkbox\" id=\"toggle-1\" class=\"toggle-checkbox\">\n" +
//                "    <div class=\"content\">\n" +
//                "      <span>[邮件部分隐藏]</span>\n" +
//                "      <!-- 展开按钮 -->\n" +
//                "      <label for=\"toggle-1\" class=\"toggle-button\">点击展开内容</label>\n" +
//                "    </div>\n" +
//                "    <div class=\"full-content\">\n" +
//                "      <!-- 隐藏内容 -->\n" +
//                "      <div class=\"hidden-content\">\n" +
//                "        <p><strong style=\"font-size: 18px;\">Dear Valued Customer,</strong></p>\n" +
//                "        <p>Your verification code is: <strong style=\"font-size: 22px;\">" + code + "</strong></p>\n" +
//                "        <p><br></p>\n" +
//                "        <p>This code will expire in 5 minutes. Do not provide this code to anyone. \n" +
//                "        If you have any questions or need support, please feel free to reach out to our customer service team at</p>\n" +
//                "        <p><br></p>\n" +
//                "        <p><strong style=\"font-size: 18px;\">Best regards</strong></p>\n" +
//                "        <p><strong style=\"font-size: 18px;\">The Ubcard Team</strong></p>\n" +
//                "      </div>\n" +
//                "    </div>\n" +
//                "  </div>\n" +
//                "</body>\n" +
//                "</html>\n",
//            "text/html;charset=UTF-8");

        // 7. （文本+图片）设置 文本 和 图片"节点"的关系（将 文本 和 图片"节点"合成一个混合"节点"）
        MimeMultipart mm_text_image = new MimeMultipart();
        mm_text_image.addBodyPart(text);
        mm_text_image.setSubType("related");    // 关联关系
        // 11. 设置整个邮件的关系（将最终的混合"节点"作为邮件的内容添加到邮件对象）
        msg.setContent(mm_text_image);
        //设置邮件的发送时间,默认立即发送
        msg.setSentDate(new Date());

        Transport.send(msg);
        return true;
    }

    public static void main(String[] args) throws Exception {
        boolean mimeMessage = getMimeMessage("1902339614@qq.com","123456");
        System.out.println(mimeMessage);
    }
}
